/**
 * gps服务器js封装
 */
function GpsooSDK() {
	this.lang="zh-cn";
	this.mapType="BAIDU";
	this.URL='http://api.gpsoo.net/1';
	this.INNERURL = CONTEXT + '/gpsoo';
	GpsooSDK.prototype.account='';
	GpsooSDK.prototype.password='';
	GpsooSDK.prototype.accessToken='';
	/**
	 * 获取accesstoken
	 * @param account 经销商的账号
	 * @param password 密码
	 * @param time UNIX时间戳
	 */
	this.loadAccesstoken=function(account, password,callback){
		var methods = "/auth/access_token";
		var ltime = parseInt(new Date().getTime()/1000);
		var signature = hex_md5(hex_md5(password)+ltime);
		var params ="?account="+account+"&time="+ltime+"&signature="+signature;
		$.ajax({
			url: this.URL+methods+params,
			type: 'GET',
			dataType: 'jsonp'
		})
		.done(function(data) {
			if(data.ret == 0){
				GpsooSDK.prototype.accessToken = data.access_token;
				GpsooSDK.prototype.account = account;
			}
			callback(data,true);
		})
		.fail(function() {
			callback(null,false);
		});
		

	};
	this.getCommonParam = function(){
		var ltime = parseInt(new Date().getTime()/1000);
		return "&access_token="+GpsooSDK.prototype.accessToken+"&account="+GpsooSDK.prototype.account+"&time="+ltime;
	};
	/**
	 * 获取一个账户名下所有设备最新位置信息
	 * @param target 要监控的账户(在汽车在线平台的账号)，目标账号名下最多只能有100个设备
	 */
	this.loadMonitor=function(target,callback){
		var methods = "/monitor";
		$.ajax({
			url: this.INNERURL + methods,
			type: 'GET',
			dataType: 'json'
		})
		.done(function(data) {
			callback(data,true);
		})
		.fail(function() {
			callback(null,false);
		});
	};
	/**
	 * 获取单个/批量设备最新位置信息
	 * @param imeis 设备imei号，多个中间用英文逗号隔开； 如果设备过多，建议采用POST方式(一次最多100个IMEI)
	 */
	this.loadTracking=function(imeis,callback){
		var methods = "/tracking";
		var params = "?imeis=" + imeis;
		$.ajax({
			url: this.INNERURL + methods + params,
			type: 'GET',
			dataType: 'json'
		})
		.done(function(data) {
			callback(data,true);
		})
		.fail(function() {
			callback(null,false);
		});
	};
	/**
	 * 获取设备历史轨迹位置信息
	 * @param imei 设备imei(一次只能一个IMEI)
	 * @param beginTime 开始时间(UTC) 秒数
	 * @param endTime 结束时间(UTC) 秒数
	 * @param limit 每次请求数据数量(一次最大1000条)
	 */
	this.loadHistory=function(imei,beginTime,endTime, limit,callback){
		var methods = "/devices/history";
		var params ="?imei="+imei+"&map_type="+this.mapType
			+ "&begin_time=" + beginTime
			+ "&end_time=" + endTime
			+"&limit="+this.getCommonParam();
		$.ajax({
			url: this.URL+methods+params,
			type: 'GET',
			dataType: 'jsonp'
		})
		.done(function(data) {
			callback(data,true);
		})
		.fail(function() {
			callback(null,false);
		});
	};
	/**
	 * 根据经纬度得到中文地址
	 * @param lng 经度
	 * @param lat 纬度
	 */
	this.loadAddress=function(lng,lat,callback){
		var methods = "/tool/address";
		var params = "?lng=" + lng + "&lat=" + lat + "&map_type=" + this.mapType + this.getCommonParam();
		$.ajax({
			url: this.URL+methods+params,
			type: 'GET',
			dataType: 'jsonp'
		})
		.done(function(data) {
			callback(data,true);
		})
		.fail(function() {
			callback(null,false);
		});
	};

	/**
	 * 查询账号下所有设备的相关信息
	 * @param target 要监控的账户(在汽车在线平台的账号)，目标账号名下最多只能有100个设备
	 */
	this.loadDevinfo = function (target, callback) {
		var methods = "/account/devinfo";
		var params = "?target=" + target + this.getCommonParam();
		$.ajax({
			url: this.URL + methods + params,
			type: 'GET',
			dataType: 'jsonp'
		})
			.done(function (data) {
				callback(data, true);
			})
			.fail(function () {
				callback(null, false);
			});

	};
	/**
	 * 根据角度获取汽车图标
	 * @param radius
	 * @returns {String}
	 */
	this.getCarImage = function (radius) {
		var imageUrl;
		if (radius == undefined || isNaN(radius) || (radius < 23 || radius > 338)) {
			imageUrl = "images/green_n_1.png";
		} else if (radius >= 23 && radius < 68) {
			imageUrl = "images/green_ne_1.png";
		} else if (radius >= 68 && radius < 113) {
			imageUrl = "images/green_e_1.png";
		} else if (radius >= 113 && radius < 158) {
			imageUrl = "images/green_se_1.png";
		} else if (radius >= 158 && radius < 203) {
			imageUrl = "images/green_s_1.png";
		} else if (radius >= 203 && radius < 248) {
			imageUrl = "images/green_sw_1.png";
		} else if (radius >= 248 && radius < 293) {
			imageUrl = "images/green_w_1.png";
		} else if (radius > 293 && radius < 338) {
			imageUrl = "images/green_nw_1.png";
		} else {
			imageUrl = "images/green_n_1.png";
		}
		return imageUrl;
	}
}


/**
 * 汽车类
 */
var Car = function(posX, posY, course){
	this.posX = posX;
	this.poxY = posY;
	this.course = course;
};
